package mode.adapteeMode05;

/**
 * 适配器模式
 * 定义 ：  适配器模式将某个类的接口转换成客户端期望的另一个接口表示，目的是消除由于接口不匹配所造成的类的兼容性问题。
 * 主要分为三类：类的适配器模式、对象的适配器模式、接口的适配器模式
 *
 *@link https://zhuanlan.zhihu.com/p/411898198
 *
 */

public class AdapteePatternDemo {

    public static void main(String[] args) {

        TargetClass objectAdaptor = new ObjectAdaptor(new AdapteeClass());

        objectAdaptor.showPersonalInfo("James","Gosling",66);

        System.out.println("=================");

        TargetClass classAdaptor = new ClassAdaptor();

        classAdaptor.showLastNameAndAge("Gosling",66);
    }

}


class AdapteeClass implements AdapteeInterface{
    public void showLastName(String lastName){
        System.out.println("Adapted showLastName ! Hello Mr." + lastName);
    }
    public void showAge(int age){
        System.out.println("Adapted showAge, age is :" + age);
    }
}



/// 使用类适配器：适配器与适配者之间是继承（或实现）关系。
class ClassAdaptor extends AdapteeClass implements TargetClass{

    @Override
    public void showFullName(String firstName, String lastName) {
        System.out.println("ClassAdaptor showFullName firstName：" + firstName + " lastName: " + lastName);
        super.showLastName(lastName);
    }

    @Override
    public void showLastNameAndAge(String lastName, int age) {
        System.out.println("ClassAdaptor showLastNameAndAge  ， lastName：" +lastName+" age: "+age);
        super.showAge(age);
    }

    @Override
    public void showPersonalInfo(String firstName, String lastName, int age) {
        this.showFullName(firstName,lastName);
        this.showLastNameAndAge(lastName,age);
    }
}

/// 使用对象适配器：适配器与适配者之间是关联关系.
class ObjectAdaptor implements TargetClass{

    private AdapteeInterface adaptee;

    public ObjectAdaptor(AdapteeInterface adaptee) {
        this.adaptee = adaptee;
    }

    @Override
    public void showPersonalInfo(String firstName , String lastName, int age){
        showFullName(firstName,lastName);
        showLastNameAndAge(lastName,age);
    }

    @Override
    public void showFullName(String firstName , String lastName) {
        System.out.println("ObjectAdaptor showFullName firstName：" + firstName+" lastName: "+lastName);
        adaptee.showLastName(lastName);
    }

    @Override
    public void showLastNameAndAge(String lastName, int age) {
        System.out.println("ObjectAdaptor showLastNameAndAge  ， lastName：" +lastName+" age: "+age);
        adaptee.showAge(age);
    }
}
